﻿using System;
using System.Collections.Generic;
using System.Linq;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Entity.Message;
using studentSubsidy.Models;

namespace studentSubsidy.Service
{
    public class MessageService
    {
        private MessageDao _messageDao = new MessageDao();
        private ApplyDao _applyDao = new ApplyDao();

        public Result sendMessage(MessageQuery messageQuery)
        {
            Apply apply = _applyDao.getApplyByApplyId(messageQuery.applyId);
            _messageDao.addMessage(messageQuery,apply.user_id);
            return new Result(200, "获取成功", null, null);
        }

        public Result getMessageById(int userId)
        {
            List<MessageReturn> messageReturns = new List<MessageReturn>();
            List<Message> messages = _messageDao.getMessageById(userId);

            foreach (var VARIABLE in messages)
            {
                MessageReturn messageReturn = new MessageReturn
                {
                    author = _messageDao.getUserById(VARIABLE.author).name,
                    cdt = VARIABLE.cdt,
                    message = VARIABLE.message
                };
                messageReturns.Add(messageReturn);
            }
            
            return new Result(200, "获取成功", messageReturns.OrderByDescending(p => DateTime.Parse(p.cdt)).ToList(), null);
        }

        public Result updateMessage(int userId)
        {
            _messageDao.updateMessage(userId);
            return new Result(200, "获取成功", "", "");
        }
    }
}